<?php
namespace app\common\process;


class StoreInfo extends Info {
	protected $model = 'tysj\\Store';
	protected $with = ['corporation', 'business_license', 'store_bank'];
	protected $append = ['is_show', 'is_examine'];

	//获取request
    public function getRequest() {
        if($this->request == null) {
            $this->request = loader()->loadAny('request', 'tysj\\Store');
        }
        return $this->request;
    }

	//获取保存数据
    public function get_save_data() {
        return $this->getRequest()->get_save_data();
    }

	public function examine2saolianshenhe($user) {
		$post = $this->getRequest()->get_private_id();

        $validate = $this->validate;
        $model = $this->model;
        if(!$validate->scene('is2saolianshenhe')->check($post)) {
        	$this->error($validate->getError());
        }
        try {
        	$model->startTrans();
        	$this->examine2saolianshenhe_ipl($post[$this->pk]);
        	$model->commit();
    	} catch(\Exception $e) {
    		$model->rollback();
    		$this->error($e->getMessage());
    	}
	}

	public function examine2saolianshenhe_ipl($id) {
		$model = $this->model;
		$row = $model->find($id);
		$enum = loadEnum('StoreExamine');
		$row->examine = $enum->code2key('saolianshenhe');
		$row->save();
	}

	//获取推广店铺列表
	public function spread_store($user) {
		$limit = input('param.limit');
		$model = $this->model;
		$model->alias('S')->join('StoreSpread SS', 'S.id = SS.sid', 'right')->where(['ss.spreadid' => $user['id']]);
		$model->privacy();
		$model->field('S.*');
		if($limit) {
			$list = $model->paginate($limit);
		} else {
			$list = $model->select();
		}
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function hide($user) {
		$model = $this->model;
		$orderModel = loadOnly('Model')->getModel('jy\\Order');

		$validate = $this->validate;
		$post = $this->getRequest()->get_private_id();

        if(!$validate->scene($this->statusScene)->check($post)) {
            $this->error($validate->getError());
        }

		try {
			$model->startTrans();
			$orderModel->startTrans();

			$result = $this->hide_ipl($post[$this->pk]);

			$model->commit();
			$orderModel->commit();
		} catch (\Exception $e) {
			$model->rollback();
			return $this->error($e->getMessage());
		}
		return $result;
	}

	public function hide_ipl($id) {
		$modelOnly = loadOnly('Model');
		$orderModel = $modelOnly->getModel('jy\\Order');

		$model = $this->model->hide($id);
		$specsModel = loadModel('tysj\\ProductSpecs');
        $userWalletModel = loadModel('tyyh\\UserWallet');

        try {
            $model->startTrans();
            $specsModel->startTrans();
            $userWalletModel->startTrans();

			$statusEnum = loadEnum('OrderStatus');
			$orderProcess = loadProcess('Order');
			$orders = $orderModel->where('sid', $id)->where('status', $statusEnum->code2key('daizhifu'))->select();
			foreach ($orders as $k => $order) {
				$orderProcess->reverse_ipl($order['id']);
			}
			
			$model->commit();
            $specsModel->commit();
            $userWalletModel->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $productSpecsModel->rollback();
            $userWalletModel->rollback();

            trace($e);
            $this->error($e->getMessage());
        }
		$this->assign('__msg', '保存成功');
        return $this->fetch();
	}

	//保存推广关系
	public function after_save($model, $post, $sid) {
		if(isset($post['spreadid'])) {
			$model = loadModel('tysj\\StoreSpread');
			$validate = loadValidate('tysj\\StoreSpread');
			$data = [
				'spreadid' => $post['spreadid'],
				'sid'      => $sid
			];
			if(!$validate->scene('save')->check($data)) {
				$this->error($validate->getError());
			}
			$examineEnum = loadEnum('StoreExamine');
	        $row = loadOnly('Model')->find('tysj\\Store', $sid);
	        $examine = $examineEnum->code2key('daishenhe');
	        //待审核状态才修改推广人信息
	        if($row['examine'] == $examine) {
				$model->where(['sid' => $sid])->delete();
				$model->save($data);
			}
		}
	}
}